﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using __ts.Core.Utilities;
using CCU.Controls;

namespace CCU
{
    internal partial class GuardianForm : Form
    {
        private static GuardianForm current = new GuardianForm();
        internal static GuardianForm Current
        {
            get
            {
                return current;
            }
        }

        public GuardianForm()
        {
            InitializeComponent();
        }

        #region RunCheckerForm events

        private void EditConfigForm_Load(object sender, EventArgs e)
        {
            if (Util.IsRuningWithAdministrator())
            {
                this.panelTop.Visible = false;
                this.Height = this.Height - this.panelTop.Height;
            }
        }

        private void EditConfigForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Visible = false;

            this.txtPassword.Text = string.Empty;
            this.txtResult.Text = "The purpose of run CCU_Guardian is to monitor CCU and make sure that CCU is running all the time.";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string userName = string.Empty;
            string password = string.Empty;

            if (!Util.IsRuningWithAdministrator())
            {
                if (TextBoxRequired.IsAllTextBoxRequiredSet(this))
                {
                    userName = this.txtUserName.Text.Trim();
                    password = this.txtPassword.Text.Trim();
                }
                else
                {
                    return;
                }
            }

            this.btnDelete.Enabled = false;
            this.btnCreate.Enabled = false;

            this.txtResult.Text = "Deleting CCU_Guardian, please wait...";

            string processName = Process.GetCurrentProcess().ProcessName;
            this.txtResult.Text = Utils.RunCmd(userName, password, "@schtasks /delete /tn " + processName.Replace(".", "_") + "_Guardian /f");

            this.btnDelete.Enabled = true;
            this.btnCreate.Enabled = true;
        }
        private void btnCreate_Click(object sender, EventArgs e)
        {
            string userName = string.Empty;
            string password = string.Empty;

            if (!Util.IsRuningWithAdministrator())
            {
                if (TextBoxRequired.IsAllTextBoxRequiredSet(this))
                {
                    userName = this.txtUserName.Text.Trim();
                    password = this.txtPassword.Text.Trim();
                }
                else
                {
                    return;
                }
            }

            this.btnDelete.Enabled = false;
            this.btnCreate.Enabled = false;
            string processName = Process.GetCurrentProcess().ProcessName;

            string cmdFile = __ts.Core.Utilities.Util.RootPath + "\\" + processName.Replace(".", "_") + "_Guardian.cmd";
            if (File.Exists(cmdFile))
            {
                FileInfo info = new FileInfo(cmdFile);
                if (info != null)
                {
                    if ((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    {
                        info.Attributes &= ~FileAttributes.ReadOnly;
                    }
                    info.Delete();
                }
            }

            using (StreamWriter sw = new StreamWriter(cmdFile, true, Encoding.Default)) // 解决中文乱码问题
            {
                sw.WriteLine("echo CCU_Guardian is processing, please wait...");
                sw.WriteLine("@tasklist|findstr /i " + processName + ".exe || start " + __ts.Core.Utilities.Util.RootPath + "\\" + processName + ".exe");
            }

            this.txtResult.Text = "Creating CCU_Guardian, please wait...";

            this.txtResult.Text = Utils.RunCmd(
                userName,
                password,
                "echo Creating CCU_Guardian, please wait...",
                //"net start|findstr \"Scheduler\">nul&&goto come",
                //"sc config Schedule start= auto >nul 2>nul",
                //"net start Schedule >nul 2>nul",
                //":come",
                "@schtasks /delete /tn " + processName.Replace(".", "_") + "_Guardian /f",
                "@schtasks /create /ru " + Utils.GetProcessUserName(Process.GetCurrentProcess().Id) + " /sc minute /mo 1 /st 00:00:00 /tn " + processName.Replace(".", "_") + "_Guardian /tr " + cmdFile
                );

            this.btnDelete.Enabled = true;
            this.btnCreate.Enabled = true;
        }

        #endregion
    }
}
